package platform

import (
	"net/http"

	"gitee.com/wangshh_123/cross-pay-go/src/middleware"
	platformCommon "gitee.com/wangshh_123/cross-pay-go/src/platform/common"
	"gitee.com/wangshh_123/cross-pay-go/src/platform/integration"
	utilsCommon "gitee.com/wangshh_123/cross-pay-go/src/utils/common"

	"github.com/gin-gonic/gin"
)

// CardCVVController 统一的卡片CVV控制器
// 使用工厂模式处理不同平台的卡片CVV查询
type CardCVVController struct {
	platformManager *integration.PlatformManager
}

// NewCardCVVController 创建卡片CVV控制器实例
func NewCardCVVController() *CardCVVController {
	return &CardCVVController{
		platformManager: integration.GetGlobalPlatformManager(),
	}
}

// GetCardCVV 获取卡片CVV信息
// 根据用户的平台类型自动选择对应的平台进行查询
func (c *CardCVVController) GetCardCVV(ctx *gin.Context) {
	// 从中间件获取平台信息
	platformInfo, exists := middleware.GetPlatformInfoFromContext(ctx)
	if !exists {
		utilsCommon.Error(ctx, http.StatusUnauthorized, "platform_info_not_found", nil)
		return
	}

	// 获取卡片ID - 支持两种参数名称
	cardID := ctx.Query("cardId")
	if cardID == "" {
		cardID = ctx.Query("card_id") // 向后兼容
	}
	if cardID == "" {
		utilsCommon.Error(ctx, http.StatusBadRequest, "card_id_required", nil)
		return
	}

	// 构建CVV查询请求
	request := &platformCommon.CardCVVRequest{
		CardID: cardID,
	}

	// 使用工厂模式处理请求
	result, err := c.platformManager.HandleRequest(ctx.Request.Context(), platformInfo.PlatformType, "card_cvv", request)
	if err != nil {
		utilsCommon.Error(ctx, http.StatusInternalServerError, "get_card_cvv_failed", err)
		return
	}

	// 返回成功响应
	utilsCommon.Success(ctx, gin.H{
		"platform": platformInfo.PlatformType,
		"data":     result,
	})
}

// GetCardCVVByPlatform 根据指定平台获取卡片CVV信息
// 允许管理员或高级用户查询指定平台的卡片CVV信息
func (c *CardCVVController) GetCardCVVByPlatform(ctx *gin.Context) {
	// 从URL参数获取平台类型
	platformType := ctx.Param("platform")
	if platformType == "" {
		utilsCommon.Error(ctx, http.StatusBadRequest, "platform_parameter_required", nil)
		return
	}

	// 获取卡片ID - 支持两种参数名称
	cardID := ctx.Query("cardId")
	if cardID == "" {
		cardID = ctx.Query("card_id") // 向后兼容
	}
	if cardID == "" {
		utilsCommon.Error(ctx, http.StatusBadRequest, "card_id_required", nil)
		return
	}

	// 构建CVV查询请求
	request := &platformCommon.CardCVVRequest{
		CardID: cardID,
	}

	// 使用工厂模式处理请求
	result, err := c.platformManager.HandleRequest(ctx.Request.Context(), platformType, "card_cvv", request)
	if err != nil {
		utilsCommon.Error(ctx, http.StatusInternalServerError, "get_card_cvv_failed", err)
		return
	}

	// 返回成功响应
	utilsCommon.Success(ctx, gin.H{
		"platform": platformType,
		"data":     result,
	})
}

// GetSupportedPlatforms 获取支持CVV查询的平台列表
func (c *CardCVVController) GetSupportedPlatforms(ctx *gin.Context) {
	// 获取平台信息
	supportedPlatforms := c.platformManager.GetFactory().GetSupportedPlatforms()

	platforms := make([]gin.H, 0, len(supportedPlatforms))
	for _, platformID := range supportedPlatforms {
		platformInfo, err := c.platformManager.GetPlatformInfo(platformID)
		if err != nil {
			continue
		}

		// 检查平台是否支持CVV查询
		supportsCVV := platformID == "photonpay" || platformID == "hlpay" // 现在两个平台都支持CVV查询

		platforms = append(platforms, gin.H{
			"platform":           platformID,
			"supports_cvv":       supportsCVV,
			"supported_features": platformInfo["supported_operations"],
			"config":             platformInfo["config"],
		})
	}

	utilsCommon.Success(ctx, gin.H{
		"platforms": platforms,
		"total":     len(platforms),
	})
}
